/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.List;
import java.util.ArrayList;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy;
import org.eclipse.gef.requests.CreateConnectionRequest;
import org.eclipse.gef.requests.ReconnectRequest;
import org.eclipse.swt.graphics.Color;

import edu.pku.notation.runtime.draw2d.TextFigure;
import edu.pku.sei.modeler.edit.parts.AbstractNodeEditPart;
import edu.pku.sei.modeler.edit.policies.StructuredLayoutEditPolicy;
import edu.pku.sei.modeler.commands.CreateLinkCommand;
import edu.pku.sei.modeler.images.ImageProvider;
import edu.pku.sei.modeler.model.core.MetaElement;
import edu.pku.sei.modeler.model.structure.MetaDiagram;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.structure.MetaLocationManager;

import edu.pku.sei.modeler.model.uml.ExtraStructuredActivities.ExpansionRegion;
import edu.pku.sei.modeler.figures.ExpansionRegionFigure;
import edu.pku.sei.modeler.model.uml.BasicActivities.ObjectFlow;
import edu.pku.sei.modeler.model.uml.BasicActivities.ControlFlow;
import edu.pku.sei.modeler.model.uml.BasicActivities.ActivityEdge;
import edu.pku.sei.modeler.model.uml.BasicInteractions.GeneralOrdering;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Message;
import edu.pku.sei.modeler.model.uml.Interfaces.InterfaceRealization;
import edu.pku.sei.modeler.model.uml.Kernel.Association;
import edu.pku.sei.modeler.model.uml.Kernel.Generalization;
import edu.pku.sei.modeler.model.uml.Kernel.PackageImport;
import edu.pku.sei.modeler.model.uml.Kernel.PackageMerge;
import edu.pku.sei.modeler.model.uml.Dependencies.Realization;
import edu.pku.sei.modeler.model.uml.Dependencies.Usage;
import edu.pku.sei.modeler.model.uml.Dependencies.Dependency;

import edu.pku.sei.modeler.model.uml.ExtraStructuredActivities.ExpansionNode;
import edu.pku.sei.modeler.model.uml.ExtraStructuredActivities.ExpansionNode;

/**
 * @generated
 *
 */
public class ExpansionRegionEditpart extends AbstractUMLNodeEditPart 
{
	public ExpansionRegionEditpart (MetaShapeElement model) {
		super(model);
	}
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		Label header = new Label("GeneratedShape");
		ExpansionRegionFigure figure = new ExpansionRegionFigure();
		figure.add(header);
		return figure;
	}

	protected DirectEditPolicy createDirectEditPolicy() {
		return null;
	}
	
	@Override
	protected IFigure getDirectEditFigure() {
		Label header = (Label)getFigure();
		return header;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
		
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new GraphicalNodeEditPolicy() {
			protected Command getConnectionCompleteCommand(CreateConnectionRequest request) {
				
				MetaLink newObj = (MetaLink)request.getNewObject();
				MetaElement ele = newObj.getModelElement();
				
				if(ele instanceof Realization) {
					CreateLinkCommand cmd = (CreateLinkCommand)request.getStartCommand();
					cmd.setTarget((MetaNode)getHost().getModel());
					return cmd;
				}
							
				if(ele instanceof Usage) {
					CreateLinkCommand cmd = (CreateLinkCommand)request.getStartCommand();
					cmd.setTarget((MetaNode)getHost().getModel());
					return cmd;
				}
							
				if(ele instanceof Dependency) {
					CreateLinkCommand cmd = (CreateLinkCommand)request.getStartCommand();
					cmd.setTarget((MetaNode)getHost().getModel());
					return cmd;
				}
							
				return null;
			}
			protected Command getConnectionCreateCommand(CreateConnectionRequest request) {
				
				MetaLink newObj = (MetaLink)request.getNewObject();
				MetaElement ele = newObj.getModelElement();
				
				if(ele instanceof Realization) {
					Command cmd = new CreateLinkCommand(newObj,
					(MetaNode)getHost().getModel());
					request.setStartCommand(cmd);
					return cmd;
				}
							
				if(ele instanceof Usage) {
					Command cmd = new CreateLinkCommand(newObj,
					(MetaNode)getHost().getModel());
					request.setStartCommand(cmd);
					return cmd;
				}
							
				if(ele instanceof Dependency) {
					Command cmd = new CreateLinkCommand(newObj,
					(MetaNode)getHost().getModel());
					request.setStartCommand(cmd);
					return cmd;
				}
							
				return null;
			}
			protected Command getReconnectSourceCommand(ReconnectRequest request) {
				return UnexecutableCommand.INSTANCE;
			}
			protected Command getReconnectTargetCommand(ReconnectRequest request) {
				return UnexecutableCommand.INSTANCE;
			}
		});
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		super.handlePropertyChanged(event);
	}
	
	private ExpansionRegion getExpansionRegion() {
		return (ExpansionRegion)((MetaNode)getModel()).getModelElement();
	}
	
	protected void refreshVisuals() {
		super.refreshVisuals();
		ExpansionRegion modelEle = (ExpansionRegion)getNode().getModelElement();
		ExpansionRegionFigure figure = (ExpansionRegionFigure)getFigure();
		
		
		
	}
}
